import api from '../../api/index'
import * as types from '../mutation-types';

//初始化一个变量
const state = {
  userInfo: [],

  contactList: [],
  contactListTotal: 0,
  userList: [],
  userListTotal: 0,

  feeDetails: [],
  feeDetailsTotal: 0,


  integralDetails: [],
  integralRanks: [],

  myFeeScrollTop: 0,

  recordigList: [],

  detailsList: [],

}

//操作：发送请求
const actions = {
  //请求个人信息
  getInfo({
    commit
  }, params) {
    api.person_info(params).then(res => {
      commit(types.GET_INFO, res);
    });
  },
  //修改个人承诺
  doPromise({
    commit
  }, params) {
    api.changepromise(params).then(res => {
      commit(types.DO_PROMISE, res);
    });
  },
  //获取党支部列表
  getContactList({
    commit
  }, params) {
    api.contact(params).then(res => {
      commit(types.GET_CONTACT, res);
    });
  },
  //获取每个党支部的人员信息列表
  getuserList({
    commit
  }, params) {
    api.queryuser(params).then(res => {
      commit(types.GET_USER_LISTS, res);

    });
  },
  //修改登录密码
  dopassword({
    commit
  }, params) {
    api.changepassword(params).then(res => {
      commit(types.CHANGE_PASSWORD, res);
    });
  },
  //获取缴纳党费明细
  getfeedetails({
    commit
  }, params) {
    api.get_fees_details(params).then(res => {
      commit(types.GET_FEE_DETAILS, res);
    });
  },
  //获取用户积分明细
  getintegraldetails({
    commit
  }, params) {
    api.get_integral_details(params).then(res => {
      commit(types.GET_INTEGRAL_DETAILS, res);
    });
  },
  //获取党员心声发布记录列表
  getrecordlist({
    commit
  }, params) {
    api.get_record_list(params).then(res => {
      commit(types.GET_RECORD_LIST, res);
    });
  },
  //获取党员心声发布记录详情
  getrecorddetails({
    commit
  }, params) {
    api.get_record_details(params).then(res => {
      commit(types.GET_RECORD_DETAILS, res);
    });
  },

  updateMyFeePosition({
    commit
  }, top) {
    commit({
      type: 'updateMyFeePosition',
      top: top
    })
  }
}

//取变量
const getters = {
  userInfo: state => state.userInfo,
  contactList: state => state.contactList,
  contactListTotal: state => state.contactListTotal,

  userList: state => state.userList,
  userListTotal: state => state.userListTotal,
  memberInfo: state => state.memberInfo,
  feeDetails: state => state.feeDetails,
  feeDetailsTotal: state => state.feeDetailsTotal,
  integralDetails: state => state.integralDetails,
  integralRanks: state => state.integralRanks,

  myFeeScrollTop: state => state.myFeeScrollTop,

  recordigList: state => state.recordigList,
  detailsList: state => state.detailsList


}

//数据操作
const mutations = {
  [types.GET_INFO](state, res) {
    //如果请求返回的是空，则继续初始化变量
    if (res.code === 0) {
      state.userInfo = []
    } else { //请求到数据，对数据进行操作
      state.userInfo = res.data
    }
  },
  [types.DO_PROMISE](state, res) {
    //如果请求返回的是空，则继续初始化变量
    if (res.code === 0) {
      state.commitments = []
    } else { //请求到数据，对数据进行操作
      state.commitments = res.data
    }
  },
  [types.GET_CONTACT](state, res) {
    //如果请求返回的是空，则继续初始化变量
    if (res.code === 0) {
      state.contactList = []
    } else { //请求到数据，对数据进行操作（操作目的？）
      state.contactList = res.data
    }
  },
  [types.GET_USER_LISTS](state, res) {
    if (res.code === 0) {
      state.userList = []
    } else {
      state.userList = res.data
    }
  },
  [types.CHANGE_PASSWORD](state, res) {},

  [types.GET_FEE_DETAILS](state, res) {
    if (res.code === 0) {
      state.feeDetails = []
      state.feeDetailsTotal = 0
    } else {
      if (res.data.data.length !== 0 && res.data.current_page !== 1) {
        let end = res.data.current_page * 10
        let start = end - 10
        for (start; start < end; start++) {
          if (start < res.data.total) {
            let newIndex = start % 10;
            state.feeDetails.push(res.data.data[newIndex])
          }
        }
      } else if (res.data.current_page == 1 && res.data.data.length !== 0) {
        state.feeDetails = res.data.data
        state.feeDetailsTotal = res.data.total
      }

    }
  },
  [types.GET_INTEGRAL_DETAILS](state, res) {
    if (res.code === 0) {
      state.integralDetails = []
      state.integralRanks = []

    } else {
      state.integralDetails = res.data.data;
      state.integralRanks = res.data.rank;
    }
  },
  [types.GET_RECORD_LIST](state, res) {
    //如果请求返回的是空，则继续初始化变量
    if (res.code === 0) {
      state.recordigList = []
    } else { //请求到数据，对数据进行操作
      state.recordigList = res.data
    }
  },
  [types.GET_RECORD_DETAILS](state, res) {
    //如果请求返回的是空，则继续初始化变量
    if (res.code === 0) {
      state.detailsList = []
    } else { //请求到数据，对数据进行操作（操作目的？）
      state.detailsList = res.data

    }
  },

  updateMyFeePosition(state, payload) {
    state.myFeeScrollTop = payload.top
  }

}

export default {
  state,
  actions,
  getters,
  mutations,
}
